import {
  RECEIVE_ADDRESS,
  RECEIVE_CATEGORYS,
  RECEIVE_SHOPS,
  RECEIVE_USER_INFO,
  RESET_USER_INFO,
  RECEIVE_GOODS,
  RECEIVE_RATINGS,
  RECEIVE_INFO,
  INCREMENT_FOOD_COUNT,
  DECREMENT_FOOD_COUNT,
  CLEAR_CART
} from "./mutation-types"
import {
  reqAddress,
  reqCategorys,
  reqShops,
  reqUserInfo,
  reqLogout,
  reqShopGoods,
  reqShopRatings,
  reqShopInfo
} from "../api/index"

export default {
  // 获取地址
  async getAddress({commit, state}) {
    let latitude = state.latitude;
    let longitude = state.longitude;
    let params = latitude + "," + longitude;
    let result = await reqAddress(params);
    console.log(result);
    commit(RECEIVE_ADDRESS, result.data)
  },
  // 获取商品分类
  async getCategorys({commit}) {
    let result = await reqCategorys();
    console.log(result);
    commit(RECEIVE_CATEGORYS, result.data)
  },
  //获取商家店面
  async getShops({commit, state}) {
    console.log(state)
    let latitude = state.latitude;
    let longitude = state.longitude;
    let data = {
      latitude,
      longitude
    }
    let result = await reqShops(data);
    console.log(result)
    commit(RECEIVE_SHOPS, result.data)
  },
  //获取用户个人信息
  recordUser({commit}, data) {
    console.log(data)
    commit(RECEIVE_USER_INFO, data)
  },
  //获取用户会话信息
  async getUserInfo({commit}) {
    let result = await reqUserInfo();
    console.log(result);
    if (result.code == 0) {
      commit(RECEIVE_USER_INFO, result.data)
    }
  },
  async getLoginOut({commit}) {
    let result = await reqLogout();
    if (result.code == 0) {
      commit(RESET_USER_INFO)
    }
  },
  async getShopGoods({commit}, callback) {
    let result = await reqShopGoods();
    console.log(result);
    if (result.code == 0) {
      commit(RECEIVE_GOODS, result.data)
      callback&callback()
    }
  },
  async getShopInfo({commit}) {
    let result = await reqShopInfo();
    console.log(result);
    if (result.code == 0) {
      commit(RECEIVE_INFO, result.data)
    }
  },
  async getShopRatings({commit}) {
    let result = await reqShopRatings();
    console.log(result);
    if (result.code == 0) {
      commit(RECEIVE_RATINGS, result.data)
    }
  },
  updateFoodCount({commit}, {isFlag, food}) {
    if (isFlag) {
      commit(INCREMENT_FOOD_COUNT, {food});
    } else {
      commit(DECREMENT_FOOD_COUNT, {food})
    }
  },
  clearCart({commit}) {
    commit(CLEAR_CART)
  }
}
